package com.example.myapplication;
import android.content.SharedPreferences;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.DB;
public class DengruActivity extends AppCompatActivity {

    private ImageView dengru_iv;
    private TextView zhuce_tv;
    private String usernameStr;
    private String passwordStr;
    private EditText nameed;
    private EditText password;
    private int remamberFlag = 0;
    private String password1 = "";
    private CheckBox mm_cb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dengru);
        SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
        initView();

        initEvent();

        if (sharedPreferences != null) {
            String name = sharedPreferences.getString("name", "");
            password1 = sharedPreferences.getString("password", "");
            remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
            nameed.setText(name);
        }
        if (remamberFlag == 1) {
            mm_cb.setChecked(true);
            password.setText(password1);
        }

    }
    private void initView(){
        dengru_iv = findViewById(R.id.dengru_iv);
        zhuce_tv = findViewById(R.id.zhuce_tv);
        nameed = findViewById(R.id.nameed);
        password = findViewById(R.id.password);
        mm_cb = findViewById(R.id.mm_cb);
    }
    private void initData(){
        usernameStr = nameed.getText().toString() + "";
        passwordStr = password.getText().toString() + "";
    }
    private void initEvent(){
        dengru_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                DB db = new DB(DengruActivity.this);
                db.loginSelect(usernameStr, passwordStr);
                editor.putString("password", passwordStr);
                editor.putString("name", usernameStr);
                if (mm_cb.isChecked()) {
                    remamberFlag = 1;
                    editor.putInt("remeber_flag", remamberFlag);
                    editor.putString("password", passwordStr);
                } else {
                    remamberFlag = 0;
                    editor.putInt("remeber_flag", remamberFlag);
                }
                editor.commit();
                Boolean result = db.loginSelect(usernameStr, passwordStr);
                if (result) {
                    Intent intent = new Intent(DengruActivity.this, MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(DengruActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(DengruActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
      zhuce_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DengruActivity.this,ZhuceActivity.class);
                startActivity(intent);

            }
        });
    }
    }




